﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.IO;
using System.Windows.Media.Animation;

namespace TouchUmlUI
{
    public class PopupMenuItem : Canvas
    {        
        public String icon;
        public int xItemPos;
        public int yItemPos;
        public String title;        
        public bool mouse_over;
        public PopupMenuBase parent;

        public void PopupItemMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            mouse_over = true;
            this.Cursor = Cursors.Hand;           
        }

        public void PopupItemMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            mouse_over = false;
            this.Cursor = Cursors.Arrow;
        }

        public PopupMenuItem(PopupMenuBase a_parent,String a_icon,String a_title,MouseButtonEventHandler handler,String item_bg_path): base()
        {
            parent = a_parent;
            mouse_over = false;            
            title = a_title;
            icon = a_icon;
            this.Width = 36;
            this.Height = 36;            

            BitmapImage source = new BitmapImage();
            source.BeginInit();
            source.UriSource = new Uri(Directory.GetCurrentDirectory() + item_bg_path);
            source.EndInit();

            Image img = new Image();
            img.Width = 36;
            img.Height = 36;
            img.Source = source;
            img.Stretch = Stretch.Fill;
            this.Children.Add(img);
            this.MouseMove += PopupItemMouseMove;
            this.MouseLeave += PopupItemMouseLeave;
                if (handler!=null) 
                    this.MouseDown += handler;
        }

    }

    public class PopupMenuBase : Canvas
    {

        static String img_bg_path = "ikony\\popupnotice.png";
        static int radius = 60;

        private ArrayList items;

        public GUML gUmlObj;
        public int xPos;
        public int yPos;
        public String title;
        private double alpha;
        private System.Windows.Forms.Timer Clock;
        public bool mouse_over;
        private HoverButton mainButton;
        public static Window1 windowParent;
      
        public void PopupMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            mouse_over = true;
            this.Cursor = Cursors.Arrow;                        
        }

        public void PopupMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            mouse_over = false;
            this.Cursor = Cursors.Arrow;           
        }

        public void setAlpha(double a_alpha)
        {
            this.Opacity = a_alpha;
            int c = 0;
            foreach (UIElement item in items) {
                double nAlpha = a_alpha - 0.2 * c;
                nAlpha = nAlpha >= 0?nAlpha:0;
                item.Opacity = nAlpha;
                c++;
            }
        }

        public bool IsMouseOver()
        {
            /*if (mouse_over) return true;
            foreach (UIElement item in items) {
                if (item.mouse_over) return true;
            }*/
            return true;
        }

        private void mouseNoticeDown(object sender, MouseEventArgs e)
        {
            Polozenie p = new Polozenie((int)(gUmlObj.umlobj.getX() + gUmlObj.umlobj.getWidth()+ 50),(int)(gUmlObj.umlobj.getY()),200,200);
            GUML guml = SignalR.utworzObiekt(UMLType.NOTICE, p, windowParent.active_canvas, null,true);
            windowParent.isAssociation(gUmlObj.umlobj as AssociableObj, guml.umlobj as AssociableObj, null);            
            windowParent.active_canvas.UpdateLayout();           
            (guml.umlobj as AssociableObj).calculateNewPoints();
            (gUmlObj.umlobj as AssociableObj).calculateNewPoints();
            guml.umlobj.updateDependencyProperties();
            gUmlObj.umlobj.updateDependencyProperties();          
        }

        public PopupMenuBase(int a_xPos, int a_yPos, String a_title) : base()
        {
            mainButton = new HoverButton("ikony\\popupnotice.png", "ikony\\popupnoticehover.png", 68, 70);
            mainButton.MouseDown += mouseNoticeDown;
            ToolTip mainTp = new ToolTip();
            mainTp.Content = ToolTipPanel.createStackPanel("Dodaj notatkę", "Dodaje notatkę do modelu oraz wiąże ją z danym obiektem");
            mainButton.ToolTip = mainTp;

            mouse_over = true;         
            this.Width = 68;
            this.Height = 70;
            this.Children.Add(mainButton);

            items = new ArrayList();
            xPos = a_xPos;
            yPos = a_yPos;
            title = a_title;
            InkCanvas.SetLeft(this, xPos);
            InkCanvas.SetTop(this, yPos);                        
            //alpha = 0;
            //setAlpha(0);
            //Clock = new System.Windows.Forms.Timer();
            //Clock.Interval = 10;
            //Clock.Tick += new EventHandler(Timer_Tick);
            //Clock.Start();            

            //img.MouseMove += PopupMouseMove;
            //img.MouseLeave += PopupMouseLeave;
        }

        public void Show()
        {                        
            DoubleAnimation[] anim = new DoubleAnimation[Children.Count];
            for (int i = 0;i<Children.Count;i++) 
            {
                anim[i] = new DoubleAnimation(0.0f, 1.0f, new TimeSpan(0, 0, 0, 0,500));
                Children[i].Visibility = Visibility.Visible;
                anim[i].CurrentStateInvalidated += delegate(object o, EventArgs e)
                {
                    Console.WriteLine(o + "");
                };
                anim[i].BeginTime = new TimeSpan(0,0,0,0,i*80);
                Console.WriteLine("UI " + Children[i]);
                Children[i].BeginAnimation(Canvas.OpacityProperty, anim[i]);                
            }
        }

        public void Timer_Tick(object sender, EventArgs eArgs)
        {
            setAlpha(alpha);
            alpha += 0.05;
            if (alpha >= 1.0)
            {
                setAlpha(1.0f);
                Clock.Stop();
            }
        }

        private void repositionItems()
        {
            int count = items.Count;
            double step = (2*Math.PI) / (double)count;            
            for (int i = 0; i < count; i++)
            {
                UIElement pi = (UIElement)items[i];
                int nx = (int)(Math.Sin(step * (i+2)) * radius);
                int ny = (int)(Math.Cos(step * (i+2)) * radius);
                int xItemPos = nx + 10;
                int yItemPos = ny + 10;
                Canvas.SetLeft(pi,xItemPos);
                Canvas.SetTop(pi,yItemPos);
            }
        }

        public void AddHoverButton(HoverButton hoverButton) 
        {
            this.Children.Add(hoverButton);
            items.Add(hoverButton);
            hoverButton.Opacity = 0.0f;
            repositionItems();
        }

        public void AddItem(PopupMenuItem a_item) 
        {
            this.Children.Add(a_item);
            items.Add(a_item);
            repositionItems();
        }

    }
}
